package com.ljs.spring_fastdfs.exception.handler;



import com.ljs.spring_fastdfs.exception.BusinessException;
import com.ljs.spring_fastdfs.exception.DataResult;
import com.ljs.spring_fastdfs.exception.code.BaseResponseCode;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;

/**
 * RestExceptionHandler
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */

/**
 * 全局捕获异常类，只要作用在@RequestMapping上，所有的异常都会被捕获
 */

@RestControllerAdvice
@Slf4j
public class RestExceptionHandler {

    /**
     * 系统繁忙，请稍候再试"
     */
    @ExceptionHandler(Exception.class)
    public DataResult handleException(Exception e) {
        log.error("Exception,exception:{}", e, e);
        return DataResult.getResult(BaseResponseCode.SYSTEM_BUSY);
    }

    /**
     * 自定义全局异常处理
     */
    @ExceptionHandler(value = BusinessException.class)
    DataResult businessExceptionHandler(BusinessException e) {
        log.error("Exception,exception:{}", e, e);
        return new DataResult(e.getMessageCode(), e.getDetailMessage());
    }

//    /**
//     * 没有权限 返回403视图
//     */
//    @ExceptionHandler(value = AuthorizationException.class)
//    public DataResult errorPermission(AuthorizationException e) {
//        log.error("Exception,exception:{}", e, e);
//        return new DataResult(BaseResponseCode.UNAUTHORIZED_ERROR);
//
//    }

    /**
     * 处理validation 框架异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    DataResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        log.error("methodArgumentNotValidExceptionHandler bindingResult.allErrors():{},exception:{}", e.getBindingResult().getAllErrors(), e);
        List<ObjectError> errors = e.getBindingResult().getAllErrors();
        return DataResult.getResult(BaseResponseCode.METHODARGUMENTNOTVALIDEXCEPTION.getCode(), errors.get(0).getDefaultMessage());
    }

    /**
     * 校验List<entity>类型， 需要controller添加@Validated注解
     * 处理Validated List<entity> 异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public DataResult handle(ConstraintViolationException exception) {
        log.error("methodArgumentNotValidExceptionHandler bindingResult.allErrors():{},exception:{}", exception, exception);
        Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
        StringBuilder builder = new StringBuilder();
        for (ConstraintViolation<?> violation : violations) {
            builder.append(violation.getMessage());
            break;
        }
        return DataResult.getResult(BaseResponseCode.METHODARGUMENTNOTVALIDEXCEPTION.getCode(), builder.toString());
    }
 //添加文件上传过大异常
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public DataResult handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e){
        log.info("文件过大");
        return  DataResult.fail("文件过大");
    }
}
